//
//  CZTabBar.swift
//  WeiBo001
//
//  Created by ZhiQiang Li on 16/7/20.
//  Copyright © 2016年 ZhiQiang Li. All rights reserved.
//

import UIKit
//声明协议
protocol CZTabBarDelegate:NSObjectProtocol{
    //协议方法
    func  composeButtonClick()
    
}
class CZTabBar: UITabBar {

    //声明代理
    weak var czDelegate:CZTabBarDelegate?
    
    var closure:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听方法
    
    func buttonClick(){
        closure?()
    }
    
    //设置视图
    private func setupUI(){
        // 添加控件
        addSubview(composeButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 计算UITabBarButton 宽度
        let w = SCREENW * 0.2
        var index: CGFloat = 0
        //UITabBarButton
        // 遍历子控件
        for value in subviews {
          
            // 判断value 是否是CZTabBarButton
       if   value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                value.frame.origin.x = index * w
                value.frame.size.width = w
                
                index++
              
                if index == 2 {
                    index++
                }
                
            }
        }
       
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5
        
          }
    
    //懒加载图片
  lazy var composeButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.sizeToFit()
        return button
        
    }()
}
